#include "seatdetection.h"

SeatDetection::SeatDetection() {}

ImageInfo&              SeatDetection::GetImageInfo(){return m_objImageInfo;}
CnnInfo&                SeatDetection::GetCnnInfo(){return m_objCnnInfo;}
DetectionWindowInfo&    SeatDetection::GetDetectionWindowInfo(){return m_objDetectionWindowInfo;}
Description&            SeatDetection::GetDescription(){return m_objDescription;}

bool SeatDetection::writeXML(const QString &filePath){
    return true;
}

bool SeatDetection::ReadXML(const QString &filePath){
    QFile file(filePath);

    if (!file.open(QIODevice::ReadOnly)) {
        return false;
    }

    QXmlStreamReader xmlReader(&file);

    m_objImageInfo.ReadXML(xmlReader);
    m_objCnnInfo.ReadXML(xmlReader);
    ReadCamerasXML(xmlReader);
    ReadCategoriesXML(xmlReader);
    m_objDetectionWindowInfo.ReadXML(xmlReader);
    m_objDescription.ReadXML(xmlReader);

    file.close();

    return true;
}

bool SeatDetection::ReadCamerasXML(QXmlStreamReader &xmlReader){
    m_vecCameraInfos.clear();

    QString className = xmlReader.name().toString();

    if (xmlReader.isStartElement() && xmlReader.name() == QStringLiteral("cameras")) {

        while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement
                 && xmlReader.name() == QStringLiteral("cameras"))) {
            xmlReader.readNext();

            if (xmlReader.isStartElement()) {
                QString className = xmlReader.name().toString();

                CameraInfo* pCameraInfo = new CameraInfo();

                pCameraInfo->ReadXML(xmlReader);

                m_vecCameraInfos.push_back(*pCameraInfo);
            }
        }
    }
    // 这个地方怎么处理
    xmlReader.readNext();
    xmlReader.readNext();
    return true;
}

bool SeatDetection::ReadCategoriesXML(QXmlStreamReader &xmlReader){
    m_vecCategoryInfos.clear();

    QString className = xmlReader.name().toString();

    if (xmlReader.isStartElement() && xmlReader.name() == QStringLiteral("categories")) {

        while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement
                 && xmlReader.name() == QStringLiteral("categories"))) {
            xmlReader.readNext();

            if (xmlReader.isStartElement()) {
                QString className = xmlReader.name().toString();

                CategoryInfo* pCategoryInfo = new CategoryInfo();

                pCategoryInfo->ReadXML(xmlReader);

                m_vecCategoryInfos.push_back(*pCategoryInfo);
            }
        }
    }
    // 这个地方怎么处理
    xmlReader.readNext();
    xmlReader.readNext();
    return true;
}

bool SeatDetection::writeJSON(const QString &filePath){
    QJsonObject obj;
    obj["imageInfo"] = m_objImageInfo.toJson();
    obj["detectionWindowInfo"] = m_objDetectionWindowInfo.toJson();
    //obj["descriptionInfo"] = m_objDescription.toJson();

    QJsonArray cameraInfosarray;
    for(int i = 0; i < m_vecCameraInfos.size(); i++){
        cameraInfosarray.append(m_vecCameraInfos[i].toJson());
    }
    obj["cameraInfos"] = cameraInfosarray;

    QJsonArray categoryInfosarray;
    for(int i = 0; i < m_vecCategoryInfos.size(); i++){
        categoryInfosarray.append(m_vecCategoryInfos[i].toJson());
    }
    obj["categoryInfos"] = categoryInfosarray;

    QJsonDocument doc(obj);
    QFile file(filePath);
    if(file.open(QIODevice::WriteOnly)){
        file.write(doc.toJson());
    }
    file.close();

    return true;
}

bool SeatDetection::readJSON(const QString &filePath){
    QFile loadFile(filePath);
    if(!loadFile.open(QIODevice::ReadOnly)){
        return false;
    }
    QByteArray saveData = loadFile.readAll();
    loadFile.close();

    QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
    if(loadDoc.isNull()){
        return false;
    }

    QJsonObject object = loadDoc.object();
    QJsonObject imageInfoObject = object["imageInfo"].toObject();
    m_objImageInfo.fromJson(imageInfoObject);
    QJsonObject detectionWindowInfoObject = object["detectionWindowInfo"].toObject();
    m_objDetectionWindowInfo.fromJson(detectionWindowInfoObject);
    QJsonObject descriptionInfoObject = object["descriptionInfo"].toObject();
    //m_objDescriptionInfo.fromJson(descriptionInfoObject);

    m_vecCameraInfos.clear();
    QJsonArray cameraInfosArray = object["cameraInfos"].toArray();
    for(int i = 0 ; i < cameraInfosArray.count();i ++){
        CameraInfo *pCameraInfo = new CameraInfo();
        pCameraInfo->fromJson(cameraInfosArray.at(i).toObject());
        m_vecCameraInfos.append(*pCameraInfo);
    }

    m_vecCategoryInfos.clear();
    QJsonArray categoryInfosArray = object["categoryInfos"].toArray();
    for(int i = 0 ; i < categoryInfosArray.count();i ++){
        CategoryInfo *pCategoryInfo = new CategoryInfo();
        pCategoryInfo->fromJson(categoryInfosArray.at(i).toObject());
        m_vecCategoryInfos.append(*pCategoryInfo);
    }

    return true;
}
